use std::collections::HashMap;
// 非常简易的用户登录系统
#[derive(PartialEq, Eq, Hash)]
struct Account<'a> {
    username: &'a str,
    password: &'a str,
}
struct AccountInfo<'a> {
    name: &'a str,
    email: &'a str,
}

type Accounts<'a> = HashMap<Account<'a>, AccountInfo<'a>>;

fn try_login<'a>(accounts: &Accounts<'a>,
    username: &'a str, password: &'a str) {
    println!("Username: {username}");
    println!("Password: {password}");
    println!("Attempting login ...");

    let login = Account {
        username,
        password,
    };
    match accounts.get(&login) {
        Some(account_info) => {
            println!("Login successful!");
            println!("Welcome, {name}!", name = account_info.name);
            println!("Your email is {email}.", email = account_info.email);
        },
        _ => println!("Login failed!"),
    }
}
fn main() {
    let mut accounts: Accounts = HashMap::new();
    let account = Account {
        username: "admin",
        password: "123456",
    };
    let account_info = AccountInfo {
        name: "Administrator",
        email: "admin@example.com",
    };
    accounts.insert(account, account_info);
    try_login(&accounts, "admin", "123456");
    println!("+++++++++++++++++++++++++++++++++++");
    try_login(&accounts, "admin", "123456");
    println!("+++++++++++++++++++++++++++++++++++");
    try_login(&accounts, "root", "123456");
}
